﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.BaseWebCtrlLib;
using HF.Business.Common;//一个业务模块的公共类
using HF.Framework;//框架类
using HF.Framework.DataClientDBAgent;
using HF.Business.Sale;//封装销售业务的类

public partial class SaleRetailYzOutStore : BaseUserControl
{
    string SnMain = "";//session值,用session来缓存细表
    DataTable SaleOutStoreTable;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load();
        SnMain = WorkflowInsId;//
        if (!IsPostBack)
        {
            initData();
        }
        SaleOutStoreTable = (DataTable)Session[SnMain];
    }
    private void initData()
    {
       
        SaleOutStoreTable = SaleRetail.GetSale_V_Retail(WorkflowInsId);
        gvOutStore.DataSource = SaleOutStoreTable;
        gvOutStore.DataBind();
        Session.Add(SnMain, SaleOutStoreTable);

    }
    private void DataBindSource()
    {
        
        Session.Add(SnMain, SaleOutStoreTable);
        gvOutStore.DataSource = SaleOutStoreTable;
        gvOutStore.DataBind();

    }

    public override void SaveUserControl(bool IsDraft)
    {
        base.SaveUserControl(IsDraft);
        if (IsDraft==false)//不是草稿
        {
           
            SaleRetailOutStoreDet srd = new SaleRetailOutStoreDet();
            foreach (DataRow dr in SaleOutStoreTable.Rows)
            {
                srd.WorkflowId = dr["WorkflowId"].ToString();
                srd.WorkflowInsId = dr["WorkflowInsId"].ToString();
                srd.WorktaskId = dr["WorktaskId"].ToString();
                srd.WorktaskInsId = dr["WorktaskInsId"].ToString();
                srd.Bill_ID = Guid.NewGuid().ToString();
                srd.Bill_Code = dr["Bill_Code"].ToString();
                srd.Product_Code = dr["Product_Code"].ToString();
                srd.Bill_Num = Convert.ToDouble(dr["Bill_NoOutNum"]);
                srd.Bill_dMemo = dr["Bill_dMemo"].ToString();
                srd.Dept_Code = SaleStock.GetDefalutStoreCode(UserId);
                srd.Status = "0";
                srd.Insert();
            }
            try
            {
                SaleRetail.SaleRetailYuzhanOutStore(srd.Bill_Code, UserId, UserName);
            }
            catch
            {
                SaleRetailOutStoreDet.DeleteByBillCode(srd.Bill_Code);//
                throw;

            }
           
        }
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
    protected void gvOutStore_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvOutStore.EditIndex = e.NewEditIndex;
        DataBindSource();
        

    }
    protected void gvOutStore_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SaleOutStoreTable.Rows[e.RowIndex]["Bill_NoOutNum"] =
            Convert.ToDouble(((TextBox)(gvOutStore.Rows[e.RowIndex].Cells[6].Controls[0])).Text);
        gvOutStore.EditIndex = -1;
        DataBindSource();
    
    }
    protected void gvOutStore_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvOutStore.EditIndex = -1;
        DataBindSource();
    }
}
